package com.example.lenovo.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.reflect.Method;

public class Main2Activity extends AppCompatActivity {
    private TextView tv_Title;
    private EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindWidget();
    }
    protected void bindWidget() {
        tv_Title = findViewById(R.id.tv_Title);
        et=findViewById(R.id.et);
        registerForContextMenu(et);
        registerForContextMenu(tv_Title);
    }

    private void setIconEnable(Menu menu, boolean enable) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            m.setAccessible(true);
            m.invoke(menu, enable);//MenuBuilder实现Menu接口，创建菜单时，传进来的menu其实就是MenuBuilder对象(java的多态特征)
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        setIconEnable(menu,true);
        switch (v.getId()){
            case R.id.et:
                menu.setHeaderTitle("菜单操作");
                menu.setHeaderIcon(R.drawable.history);
                MenuItem menuItem = menu.add(Menu.NONE, Menu.FIRST + 1, 1, "菜单一");
                menuItem.setIcon(R.drawable.update);
                menuItem = menu.add(Menu.NONE, Menu.FIRST + 2, 2, "菜单二");
                menuItem.setIcon(R.drawable.about);
                SubMenu subMenu = menu.addSubMenu(Menu.NONE, Menu.FIRST + 3, 3, "菜单三");
                subMenu.setIcon(R.drawable.exit);
                subMenu.add(Menu.NONE, Menu.FIRST + 4, 1, "二级菜单1").setIcon(R.drawable.update);
                subMenu.add(Menu.NONE, Menu.FIRST + 5, 2, "二级菜单2").setIcon(R.drawable.about);

                break;
            case R.id.tv_Title:
                menu.setHeaderTitle("菜单");
                menu.setHeaderIcon(R.drawable.history);
                MenuItem menuItem1= menu.add(Menu.NONE, Menu.FIRST + 1, 1, "一");
                menuItem1.setIcon(R.drawable.update);
                menuItem1 = menu.add(Menu.NONE, Menu.FIRST + 2, 2, "二");
                menuItem1.setIcon(R.drawable.about);
                SubMenu subMenu1 = menu.addSubMenu(Menu.NONE, Menu.FIRST + 3, 3, "三");
                subMenu1.setIcon(R.drawable.exit);
                subMenu1.add(Menu.NONE, Menu.FIRST + 4, 1, "二级菜单1").setIcon(R.drawable.update);
                subMenu1.add(Menu.NONE, Menu.FIRST + 5, 2, "二级菜单2").setIcon(R.drawable.about);

                break;


        }
        super.onCreateContextMenu(menu, v, menuInfo);

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST + 1:
                tv_Title.setText("菜单一");
                break;
            case Menu.FIRST + 2:
                tv_Title.setText("菜单二");
                break;
            case Menu.FIRST + 3:
                tv_Title.setText("菜单三");
                break;
            case Menu.FIRST + 4:
                tv_Title.setText("二级菜单1");
                break;
            case Menu.FIRST + 5:
                tv_Title.setText("二级菜单2");
                break;
            default:
                return super.onContextItemSelected(item);
        }
        return  true;
    }

}
